--[[
从objdump 获得函数名
    生成gdb脚本
]]

local objdump_path = "arm-none-eabi-objdump"
local elf = "hack.elf";
if(arg[2])then
    elf = arg[2];
end
local format = string.format;
local f = io.popen(string.format("%s -d %s",objdump_path,elf));
assert(f);
local s = f:read("*a");
--print(s);
local out = io.stdout;
out = io.open(arg[1],"w+");

--[

local list = {};
local f = io.open("test.s","w+");
f:write(s);
for target,a,addr,reg in string.gmatch(s,"(%x+)%s*%<([A]?)JMP(%x+)_(r%d+).%:")do
    --print(target,addr,reg);
    addr = tonumber('0x'..addr);
    print(string.format('addr:%x,=%s',addr,target),a);

    --生成跳转汇编
    local jmps = [[
    .thumb
    ldr %s,=0x%s+1
    bx %s
]]
    if(a=='A')then
        jmps = [[
    .arm
    ldr %s,=0x%s
    bx %s
]]
    end

    jmps = format(jmps,reg,target,reg);
    local file = io.open(format("%x.s",addr),"w+");
    file:write(jmps);


    --[[
    生成gdb脚本
    格式为
        load_asm %x.s addr
    ]]
    out:write(format([[
    load_asm %x.s 0x%x
    ]],
    addr,addr));
end



